import WeappRequest from "./request"
import { getStorage, toast, clearStorage } from './storage'
const http = new WeappRequest({ timeout: 10000, baseUrl: 'http://172.21.1.53:8600' });
http.interceptors.request = options => {
  let token = getStorage(wx.$TOKEN_NAME);
  if (token)
    options.header["Authorization"] = token;
  options.header["Author"] = "XYZ";
};
http.interceptors.response = res => {
  let { success, statusCode, data: { message = "网络异常，请重试……", code } = {}, errMsg } = res;
  //未登录用户
  if (success && (statusCode === 200 || statusCode === 500) && code === 401) {
    clearStorage();
    wx.redirectTo({
      url: '/pages/index/index',
    })
  }
  if (message === "")
    message = "网络异常，请重试……";
  statusCode !== 200 && toast({ title: message });
  !success && toast({ title: errMsg });
  return res;
}
export default http;